<?php
include_once 'Util/functions.php';
class descTable {
	public $Field;
	public $Type;
	public $Null;
	public $Key;
	public $Default;
	public $Extra;
	public $attribute;
	public $newType;
	public $newTypeSize;
	public $insertFlag;
	public $updateFlag;
	public $singleQuotes;
	public $sprintfStr;
	public $escapeStr;
	public $paramName;
	public $updateParamName;
	public $fixedValue;
}

class ConditionSetting {
	public $fieldName;
	public $valueName;
	public $operator;
	public $likeHeadStr;
	public $likeTailStr;
	public $singleQuotes = "'";
	public $attribute;
	public $escapeStr;
	public $sprintfStr;

}
class TableObject {
	
	public $data;
	public $tableName;
	public $aliasName;
	public $columnSize;
	public $definedTableName;
}
function IsNumberType($ty) {
	if ($ty == "tinyint" || $ty == "smallint" || $ty == "mediumint" || $ty == "int" || $ty == "bigint" || $ty == "decimal" || $ty == "float" || $ty == "double" || $ty == "double" || $ty == "bit" || $ty == "tinyint" || $ty == "bigint" || $ty == "int") {
		return true;
	}
	else {
		return false;
	}
}

require "configs/smartmain.php";
include_once "configs/Mysql.class.php";

$columns = array ();
$insertColumn = array ();

$updateColum = array ();

$tableObjects = array ();

$conditions = array ();

$fixVaGroup = array ();

$action = $_REQUEST ["act"];
//echo "action:$action<br //>";
$sortOption = $_REQUEST ["sortOption"];

$sortArray = array ();

for($i1 = 0; $i1 < $sortOption; $i1 ++) {
	$sortArray [$i1] = $i1 + 1;

}

$c2 = 0;
$c3 = 0;

//echo "start <p>";


$tablesSize = $_REQUEST ["tableSize"];

///for insert method
$returnAutoIncreasingIDFlag = 0;
$autoIncreasingID = "";
///


for($tc = 0; $tc < $tablesSize; $tc ++) {
	
	$tableObjec = new TableObject ( );
	$tableObjec->tableName = $_REQUEST ["tableName" . $tc];
	$tableObjec->aliasName = $_REQUEST ["aliasName" . $tc];
	$tableObjec->definedTableName = $_REQUEST ["definedTableName" . $tc];
	
	$tableObjec->columnSize = $columnSize = $_REQUEST ["columnSize" . $tc];
	//echo "co:".$columnSize."<p>";
	for($cc = 0; $cc < $columnSize; $cc ++) {
		$counter = "" . $tc . "_" . $cc;
		//echo "need".$counter."<p>";
		$one = new descTable ( );
		$one->Field = $_REQUEST ["field" . $counter];
		$one->attribute = $_REQUEST ["attribute" . $counter];
		//echo "attribute:".$one->attribute."<p>";
		$one->newType = $_REQUEST ["newType" . $counter];
		$one->newTypeSize = $_REQUEST ["newTypeSize" . $counter];
		$one->Extra = $_REQUEST ["extra" . $counter];
		//echo "attr:".$one->attribute." newtype:".$one->newType.":[";
		

		if (strstr ( $one->newType, "int" )) {
			$one->singleQuotes = "";
			$one->sprintfStr = "%d";
		}
		else {
			$one->singleQuotes = "'";
			$one->sprintfStr = "%s";
		}
		//echo $one->singleQuotes."]<br //>";
		$one->insertFlag = 1;
		$one->updateFlag = 1;
		if ($one->Extra == "auto_increment") {
			if (isset ( $_REQUEST ["insertAutoIncreasingID"] )) {
			
			}
			else {
				
				$one->insertFlag = 0;
				$returnAutoIncreasingIDFlag = 1;
				$autoIncreasingID = $one->attribute;
			}
			if (isset ( $_REQUEST ["updateAutoIncreasingID"] )) {
			
			}
			else {
				
				$one->updateFlag = 0;
			
			}
		
		}
		
		if (isset ( $_REQUEST ["escapeStr" . $counter] )) {
			if (IsNumberType ( $one->newType )) {
				$one->escapeStr = 0;
			}
			else {
				$one->escapeStr = 1;
			}
		}
		else {
			$one->escapeStr = 0;
		}
		
		if (isset ( $_REQUEST ["need" . $counter] )) {
			if (IsNoEmpty ( $_REQUEST ['fixedValue' . $counter] )&&($one->insertFlag||$one->updateFlag == 1)) {
				$one->fixedValue = $_REQUEST ['fixedValue' . $counter];
				$fixVaGroup [] = $one;
			}
			else {
				if ($one->insertFlag == 1) {
					$one->paramName = '$para_insert_' . $one->Field;
					$insertColumn [] = $one;
				
				}
				if ($one->updateFlag == 1) {
					$one->updateParamName = '$para_update_' . $one->Field;
					$updateColum [] = $one;
				}
				
				$columns [] = $one;
			}
		}
		
		if (isset ( $_REQUEST ["equalCondition" . $counter] )) {
			//echo "cond:".$c1."=".$one->Field."  is equal to ".$conditionField[$counter]."<br>";
			$conditions [$c3] = new ConditionSetting ( );
			$conditions [$c3]->operator = "=";
			$conditions [$c3]->fieldName = $one->Field;
			$conditions [$c3]->attribute = $one->attribute;
			$conditions [$c3]->valueName = "cond_para_equal_" . $_REQUEST ["equalCondition" . $counter];
			$conditions [$c3]->singleQuotes = $one->singleQuotes;
			$conditions [$c3]->escapeStr = $one->escapeStr;
			$conditions [$c3]->sprintfStr = $one->sprintfStr;
			
			$c3 ++;
		
		}
		else if (isset ( $_REQUEST ["minCondition" . $counter] ) || isset ( $_REQUEST ["maxCondition" . $counter] )) {
			
			///$biggerCondition
			if (isset ( $_REQUEST ["minCondition" . $counter] )) {
				//echo "cond:".$c1."=".$one->Field."  is equal to ".$conditionField[$counter]."<br>";
				$conditions [$c3] = new ConditionSetting ( );
				if (isset ( $_REQUEST ["minAndEqualCondition" . $counter] )) {
					$conditions [$c3]->operator = ">=";
					$conditions [$c3]->valueName = "cond_para_minequal_" . $_REQUEST ["minCondition" . $counter];
				}
				else {
					$conditions [$c3]->operator = ">";
					$conditions [$c3]->valueName = "cond_para_min_" . $_REQUEST ["minCondition" . $counter];
				
				}
				$conditions [$c3]->fieldName = $one->Field;
				$conditions [$c3]->attribute = $one->attribute;
				$conditions [$c3]->singleQuotes = $one->singleQuotes;
				$conditions [$c3]->escapeStr = $one->escapeStr;
				$conditions [$c3]->sprintfStr = $one->sprintfStr;
				$c3 ++;
			
			}
			//$lessCondition
			if (isset ( $_REQUEST ["maxCondition" . $counter] )) {
				//echo "cond:".$c1."=".$one->Field."  is equal to ".$conditionField[$counter]."<br>";
				$conditions [$c3] = new ConditionSetting ( );
				if (isset ( $_REQUEST ["maxAndEqualCondition" . $counter] )) {
					$conditions [$c3]->operator = "<=";
					$conditions [$c3]->valueName = "cond_para_maxequal_" . $_REQUEST ["maxCondition" . $counter];
				}
				else {
					$conditions [$c3]->operator = "<";
					$conditions [$c3]->valueName = "cond_para_max_" . $_REQUEST ["maxCondition" . $counter];
				
				}
				$conditions [$c3]->fieldName = $one->Field;
				$conditions [$c3]->attribute = $one->attribute;
				$conditions [$c3]->singleQuotes = $one->singleQuotes;
				$conditions [$c3]->escapeStr = $one->escapeStr;
				$conditions [$c3]->sprintfStr = $one->sprintfStr;
				$c3 ++;
			
			}
		}
		else if (isset ( $_REQUEST ["likeHeadCondition" . $counter] ) || isset ( $_REQUEST ["likeTailCondition" . $counter] )) {
			//$likeHeadCondition
			$conditions [$c3] = new ConditionSetting ( );
			$conditions [$c3]->fieldName = $one->Field;
			$conditions [$c3]->operator = " like ";
			$conditions [$c3]->valueName = "cond_para_like_" . $one->attribute;
			$conditions [$c3]->attribute = $one->attribute;
			$conditions [$c3]->escapeStr = $one->escapeStr;
			$conditions [$c3]->sprintfStr = $one->sprintfStr;
			
			if (isset ( $_REQUEST ["likeHeadCondition" . $counter] )) {
				//echo "cond:".$c1."=".$one->Field."  is equal to ".$conditionField[$counter]."<br>";
				$conditions [$c3]->likeHeadStr = "%%";
			
			}
			//$likeTailCondition
			if (isset ( $_REQUEST ["likeTailCondition" . $counter] )) {
				$conditions [$c3]->likeTailStr = "%%";
			}
			$c3 ++;
		}
	
	}
	$tableObjects [] = $tableObjec;
}

$className = $_REQUEST ["className"];

$template->assign ( "whereStrs", $conditions );
//$template->assign("className","DataClass_".$tableName);
//$template->assign("loadColumns",$loadNames);
//$template->assign("tableName",$tableName);
$template->assign ( "sortArray", $sortArray );
$template->assign ( "className", $className );
$template->assign ( "attributes", $columns );
$template->assign ( "conditionsSize", sizeof ( $conditions ) );

$template->assign ( "tables", $tableObjects );

//echo '<pre>'.print_r($_REQUEST,1).'</pre>';
//print_r($action);

if ($action == "search") {
	$fn = $_REQUEST ["sfunctionName"];
	
	if ($fn == "") {
		$fn = " UnDfineName";
	}
	$template->assign ( "functionName", $fn );
	$template->assign ( "whereStrs", $conditions );
	$template->assign ( "joinSql", $_REQUEST ["joinSql"] );
	if (isset ( $_REQUEST ["createPageFunction"] )) {
		$template->assign ( "createPageFunction", $_REQUEST ["createPageFunction"] );
	}
	else {
		$template->assign ( "createPageFunction", 0 );
	}
	//echo "---";
	$template->display ( 'CreateSearchFunction.tpl.html' );
}
else if ($action == "load") {
	$fn = $_REQUEST ["lfunctionName"];
	
	if ($fn == "") {
		$fn = " UnDfineName";
	}
	$template->assign ( "functionName", $fn );
	$template->assign ( "joinSql", $_REQUEST ["joinSql"] );
	$template->assign ( "whereStrs", $conditions );
	//echo count($conditions);
	$template->display ( 'CreateLoadFunction.tpl.html' );
}
else if ($action == "isExist") {
	$fn = $_REQUEST ["iefunctionName"];
	
	if ($fn == "") {
		$fn = " UnDfineName";
	}
	$template->assign ( "functionName", $fn );
	$template->assign ( "joinSql", $_REQUEST ["joinSql"] );
	$template->assign ( "whereStrs", $conditions );
	$template->display ( 'CreateIsExistFunction.tpl.html' );
}
else if ($action == "PhpClass") {
	
	$template->assign ( "dbClassPath", $_REQUEST ["dbClassPath"] );
	$template->assign ( 'info', $_REQUEST ["info"] );
	
	ob_start();
	$template->display ( 'CreatePhpClass.tpl.html' );
	$content=ob_get_contents();
	
	if(!empty($_REQUEST['class_file_download_mode'])){
		//download file
		require_once 'html2text.php';

		$cov=new html2text($content);
		//$cov->set_html();
		$content=$cov->get_text();
		
		header("Content-Disposition: attachment; filename=".$className.'.php');
		header("Content-type:text/text");
		header("Accept-Ranges:bytes");
		header("Accept-Length:".strlen($content));
		
		echo $content;
		ob_end_clean();
	}
	else{
		ob_end_flush();
	}
	
}
else if ($action == "helpCode") {
	
	$template->assign ( "smartName", $_REQUEST ["smartName"] );
	$template->assign ( "inputTextLimit", $_REQUEST ["inputTextLimit"] );
	$template->assign ( "rowSize", count ( $columns ) );
	
	$template->display ( 'OtherHelper.tpl.html' );
}
else if ($action == "updateMethod") {
	
	//echo $tablesSize."<br //>";
	if ($tablesSize != 1) {
		$template->assign ( "message", "The method is designed only for one table!" );
		$template->display ( 'message.tpl.html' );
		return;
	
	}
	
	$template->assign ( "whereStrs", $conditions );
	$template->assign ( "updatedAttributes", $updateColum );
	$template->assign ( "tableObj", $tableObjects [0] );
	$template->assign ( 'fixedVG', $fixVaGroup );
	$template->assign ( "functionName", $_REQUEST ["updateFunctionName"] );
	$template->display ( 'CreateUpdateFunction.tpl.html' );

}
else if ($action == "deleteMethod") {
	
	//echo $tablesSize.$_REQUEST["deleteFunctionName"] ."<br //>";
	if ($tablesSize != 1) {
		$template->assign ( "message", "The method is designed only for one table!" );
		$template->display ( 'message.tpl.html' );
		return;
	
	}
	
	$template->assign ( "whereStrs", $conditions );
	$template->assign ( "tableObj", $tableObjects [0] );
	
	$template->assign ( "functionName", $_REQUEST ["deleteFunctionName"] );
	$template->display ( 'CreateDeleteFunction.tpl.html' );
}

else if ($action == "insertMethod") {
	
	//echo $tablesSize."<br //>";
	if ($tablesSize != 1) {
		$template->assign ( "message", "The method is designed only for one table!" );
		$template->display ( 'message.tpl.html' );
		return;
	
	}
	
	$template->assign ( "returnAutoIncreasingIDFlag", $returnAutoIncreasingIDFlag );
	$template->assign ( "autoIncreasingID", $autoIncreasingID );
	$template->assign ( "tableObj", $tableObjects [0] );
	$template->assign ( 'fixedVG', $fixVaGroup );
	$template->assign ( "insertedColumns", $insertColumn );
	$template->assign ( "functionName", $_REQUEST ["creatFunctionName"] );
	$template->display ( 'CreateInsertFunction.tpl.html' );
}

?>
